package com.zx.notification.admin.controller;

import com.zx.core.wrapper.ListWrapper;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.notification.admin.service.IInboxReadRecordsService;
import com.zx.notification.common.param.InboxReadRecordsParam;
import com.zx.notification.common.vo.InboxReadRecordsVO;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * description: 站内信阅读记录管理控制器 <br>
 * create: 2025-10-20
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/inbox-read-records")
@RequiredArgsConstructor
public class InboxReadRecordsController {

    private final IInboxReadRecordsService iInboxReadRecordsService;

    /**
     * 分页查询站内信阅读记录列表
     *
     * @param inboxReadRecordsParam 站内信阅读记录查询参数
     * @return 站内信阅读记录列表
     */
    @PostMapping("/list")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "站内信阅读记录参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Query.class})
    })
    @PreAuthorize("hasAuthority('notify:inbox:list')")
    public PageWrapper<InboxReadRecordsVO> list(@RequestBody InboxReadRecordsParam inboxReadRecordsParam) {
        return iInboxReadRecordsService.getInboxReadRecordsVOListByPage(inboxReadRecordsParam);
    }

    /**
     * 根据用户ID和阅读状态查询站内信阅读记录列表
     *
     * @param userId     用户ID
     * @param readStatus 阅读状态
     * @return 站内信阅读记录列表
     */
    @GetMapping("/query")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "用户ID", rules = {ValidateRulesEnum.NOT_NULL}),
    })
    @PreAuthorize("isAuthenticated()")  // 任何已认证用户都可以调用
    public ListWrapper<InboxReadRecordsVO> query(@RequestParam("userId") String userId,
                                                 @RequestParam(value = "readStatus", required = false) String readStatus) {
        return iInboxReadRecordsService.getInboxReadRecordsListByUserIdAndStatus(userId, readStatus);
    }

    /**
     * 更新通知的阅读状态
     *
     * @param notificationId 通知ID
     * @param readStatus     阅读状态
     * @return 响应结果
     */
    @PostMapping("/update-status")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "通知ID", rules = {ValidateRulesEnum.NOT_NULL}),
            @ParameterValidation(index = 1, desc = "阅读状态", rules = {ValidateRulesEnum.NOT_NULL})
    })
    @PreAuthorize("isAuthenticated()")  // 任何已认证用户都可以调用
    public ResponseWrapper updateStatus(@RequestParam("notificationId") String notificationId,
                                        @RequestParam("readStatus") String readStatus) {
        return iInboxReadRecordsService.updateReadStatus(notificationId, readStatus);
    }

    /**
     * 获取指定站内信阅读记录信息
     *
     * @param inboxReadRecordId 站内信阅读记录ID
     * @return 站内信阅读记录信息
     */
    @GetMapping("/info/{inboxReadRecordId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "站内信阅读记录ID", rules = {ValidateRulesEnum.NOT_NULL})
    })
    @PreAuthorize("hasAuthority('notify:inbox:detail')")
    public ObjectWrapper<InboxReadRecordsVO> info(@PathVariable("inboxReadRecordId") String inboxReadRecordId) {
        return iInboxReadRecordsService.selectInboxReadRecordById(inboxReadRecordId);
    }

    /**
     * 删除站内信阅读记录
     *
     * @param inboxReadRecordId 站内信阅读记录ID
     * @return 响应结果
     */
    @DeleteMapping("/delete/{inboxReadRecordId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "站内信阅读记录ID", rules = {ValidateRulesEnum.NOT_NULL})
    })
    @PreAuthorize("hasAuthority('notify:inbox:remove')")
    public ResponseWrapper delete(@PathVariable("inboxReadRecordId") String inboxReadRecordId) {
        return iInboxReadRecordsService.deleteInboxReadRecordById(inboxReadRecordId);
    }
}